package com.atguigu.gmall.pay.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.mq.config.RabbitService;
import com.atguigu.gmall.pay.service.AliPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping(value = "/ali")
public class AliPayController {
    @Autowired
    private AliPayService aliPayService;
    @Autowired
    private RabbitService rabbitService;

    /**
     * 阿里下单接口
     * @param orderId
     * @return
     */
    @GetMapping(value = "/pay/{orderId}")
    public String aliPay(@PathVariable(value = "orderId")Long orderId){
        return aliPayService.aliPay(orderId);
    }

    /**
     * 支付宝同步回调地址
     * @param map
     * @return
     */
    @GetMapping(value = "/callback/return")
    public String aliCallBack(@RequestParam Map<String,String> map){
        System.out.println(map);
        return "支付宝回调成功";
    }

    /**
     * 异步回调地址
     * @param map
     * @return
     */
    @GetMapping(value = "/callback/notify")
    public String notify(@RequestParam Map<String,String> map){
        //发送一条消息
        rabbitService.sendMessage("pay_exchange","pay.order.zfb",
                JSONObject.toJSONString(map));
        return "success";
    }

    /**
     * 校验支付结果
     * @param orderId
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/close/{orderId}")
    public String close(@PathVariable(value = "orderId")Long orderId) throws Exception{
        return aliPayService.close(orderId);
    }

    /**
     * 校验退款结果
     * @param orderId
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/refund/{orderId}")
    public String refund(@PathVariable(value = "orderId")Long orderId) throws Exception{
        return aliPayService.refund(orderId);
    }
}
